package cn.bjc.order.service.impl;

import cn.bjc.basic.service.impl.BaseServiceImpl;
import cn.bjc.basic.util.PageList;
import cn.bjc.order.domain.OrderPetAcquisition;
import cn.bjc.order.dto.OrderDto;
import cn.bjc.order.mapper.OrderPetAcquisitionMapper;
import cn.bjc.order.query.OrderPetAcquisitionQuery;
import cn.bjc.order.service.IOrderPetAcquisitionService;
import cn.bjc.org.domain.Employee;
import cn.bjc.org.domain.Shop;
import cn.bjc.org.mapper.EmployeeMapper;
import cn.bjc.org.mapper.ShopMapper;
import cn.bjc.pet.domain.Pet;
import cn.bjc.pet.mapper.PetMapper;
import cn.bjc.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Autowired
    private ShopMapper shopMapper;
    
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;
    
    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, Long id) {
        /**
         * shopId
         * empId
         */
        Employee employee = employeeMapper.loadByLogininfoId(id);
        if(employee.getShop_id()!=null){

            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdmin_id()!=employee.getId()){
                query.setEmpId(employee.getId());
            }
            // 店铺人员
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    @Override
    public void confirm(OrderDto dto) {
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();
        /**思路
         * 1.该订单状态,金额,支付方式
         * 2.改寻主消息状态
         * 3.改pet金额
         */
        
        //1改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(orderId);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        
        //2改寻主消息状态
        searchMasterMsgMapper.updateState(orderPetAcquisition.getSearch_master_msg_id());
        
        //3改pet金额
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);
        
    }

    //取消订单
    @Override
    public void cancel(Long id) {
        /**
         * 1.修改订单状态
         * 2.寻主消息的shop_id=null
         * 3.删除pet表
         */
        //1改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);    //-1:取消
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        
        //2修改shop_id
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearch_master_msg_id());
        
        //3删除pet表
        petMapper.remove(orderPetAcquisition.getPet_id());
        
        
    }
}
